package cn.lg.soar.common.util.jackson.deserializer;

import cn.lg.soar.common.util.DatetimeUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * LocalTime 反序列化
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class SLocalTimeDeserializer extends JsonDeserializer<LocalTime> {


    private final DateTimeFormatter formatter;

    public SLocalTimeDeserializer() {
        this(DatetimeUtil.FORMAT_DATE);
    }

    public SLocalTimeDeserializer(String pattern) {
        this(DateTimeFormatter.ofPattern(pattern));
    }

    public SLocalTimeDeserializer(DateTimeFormatter formatter) {
        this.formatter = formatter;
    }

    @Override
    public LocalTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        return DatetimeUtil.toLocalTime(parser.getText(), formatter);
    }
}
